package models

import (
	"fmt"
	"football/src/utils"
	"time"
)

type OddsDataInfoBasic struct {
	ID        string    `json:"id" gorm:"primaryKey"`                // 唯一ID
	StartTime string    `json:"starttime" gorm:"column:starttime"`   // 开始时间
	League    string    `json:"league" gorm:"column:league"`         // 联赛
	Event     string    `json:"event" gorm:"column:event"`           // 比赛
	EventId   string    `json:"eventID" gorm:"column:event_id"`      // 比赛ID
	Dealer    string    `json:"dealer" gorm:"column:dealer"`         // 公司
	Lay       string    `json:"lay" gorm:"column:lay"`               // 卖出
	BetType   string    `json:"betType" gorm:"column:bet_type"`      // 玩法
	Handicap  string    `json:"handicap" gorm:"column:handicap"`     // 盘口
	OddHome   string    `json:"odd_home" gorm:"column:odd_home"`     // 主赔率
	OddDraw   string    `json:"odd_draw" gorm:"column:odd_draw"`     // 平赔率
	OddAway   string    `json:"odd_away" gorm:"column:odd_away"`     // 客赔率
	Currency  string    `json:"currency" gorm:"column:currency"`     // 货币
	MaxAmount string    `json:"max_amount" gorm:"column:Max_amount"` // 最大投注
	MinAmount string    `json:"min_amount" gorm:"column:Min_amount"` // 最小投注
	Other     string    `json:"other" gorm:"column:other"`           // 其他
	Timestamp time.Time `json:"timestamp" gorm:"column:timeStamp"`   // 更新时间
}

type League struct {
	ID           string `json:"id"`
	LeagueCNName string `json:"league_cn_name"`
	LeagueENName string `json:"league_en_name"`
}

func (table *League) TableName() string {
	return "soccer_league"
}

func (table *OddsDataInfoBasic) TableName() string {
	return "history_odds_data_info"
}

func GetFixturesList(page int, limit int, date string) ([]*OddsDataInfoBasic, int, error) {
	query := utils.Football.Model(&OddsDataInfoBasic{})
	switch date {
	case "today":
		query = query.Where("DATE(starttime) = DATE(?)", utils.GetTodayStart().Format("2006-01-02"))
	case "tomorrow":
		query = query.Where("DATE(starttime) = DATE(?)", utils.GetTomorrowStart().Format("2006-01-02"))
	case "yesterday":
		query = query.Where("DATE(starttime) = DATE(?)", utils.GetYesterdayStart().Format("2006-01-02"))
	case "week":
		query = query.Where("starttime >= DATE(?)", utils.GetWeekStart())
	default:
		layout := "2006-01-02 15:04"
		parsedTime, err := time.Parse(layout, date)
		if err != nil {
			fmt.Println("Error parsing time:", err)
			return nil, 0, err
		}
		query = query.Where("starttime = ?", parsedTime)
	}

	return utils.Paginate[OddsDataInfoBasic](query, page, limit)
}

func GetLeagueList(page int, limit int) ([]*League, int, error) {
	query := utils.Football.Model(&League{})
	return utils.Paginate[League](query, page, limit)
}
